package com.gopay.common.domain.bank.ccb.cbrmb.req;

import java.io.Serializable;

import org.apache.log4j.Logger;

import com.gopay.common.domain.bank.ccb.cbrmb.ByteData;
import com.gopay.common.util.cbrmb.MsgUtils;

/**
 * 
 * @ClassName: CbRmbReqMsg
 * @Description: 通知报文数据
 * @author gd.cui
 * @date 2016年6月29日 上午11:30:17 ww.gopay.com.cn Inc.All rights reserved.
 */
public class CbRmbNotificationMsg implements ByteData, Serializable {

    /**
     * @Fields serialVersionUID :
     */
    private static final long serialVersionUID = 1160861497105169291L;

    private static Logger logger = Logger.getLogger(CbRmbNotificationMsg.class);

    public static final int MSG_NOTIFICATION_LENGTH = 137;

    /**
     * 通知方标识 建行发起固定-CCBS； 支付机构发起固定-THPS;
     */
    private String senderId;

    /**
     * 交易类型 01-来盘，02-回盘
     */
    private String tranType;
    /**
     * 交易日期 yyyyMMdd
     */
    private String tranData;
    /**
     * 交易时间 HHmmss
     */
    private String tranTime;
    /**
     * 交易流水 唯一标识每一笔通知，yyyyMMddHHmmss+3位序号
     */
    private String tranSn;
    /**
     * 文件名 来盘文件名按KJZFLP_yyyyMMdd_三位序号.xml； 回盘文件按KJZFHP_yyyyMMdd_三位序号.xml； 不带目录路径
     * 
     */
    private String fileName;
    /**
     * mac 存来盘/回盘文件SHA1校验值，用于内容有效性校验，40位长度大写表示
     */
    private String mac;

    private byte[] senderIdByte = new byte[4];

    private byte[] tranTypeByte = new byte[2];

    private byte[] tranDataByte = new byte[8];

    private byte[] tranTimeByte = new byte[6];

    private byte[] tranSnByte = new byte[17];

    private byte[] fileNameByte = new byte[60];

    private byte[] macByte = new byte[40];
    
    public CbRmbNotificationMsg() {
    }


    public CbRmbNotificationMsg(byte[] source) {
        initPropertiesByBytes(source);
    }

    @Override
    public byte[] getByteData() {
        covertPropertiesToBytes();
        return MsgUtils.mergeBytes(this.senderIdByte, this.tranTypeByte, this.tranDataByte, this.tranTimeByte,
                this.tranSnByte, this.fileNameByte, this.macByte);
    }

    private void covertPropertiesToBytes() {
        byte[] senderIdSource = this.senderId.getBytes();
        int senderIdLenth = senderIdSource.length;
        MsgUtils.copyBytes(senderIdSource, this.senderIdByte, 1, senderIdLenth, 1);

        byte[] tranTypeSource = this.tranType.getBytes();
        int tranTypeLenth = tranTypeSource.length;
        MsgUtils.copyBytes(tranTypeSource, this.tranTypeByte, 1, tranTypeLenth, 1);

        byte[] tranDataSource = this.tranData.getBytes();
        int tranDataLenth = tranDataSource.length;
        MsgUtils.copyBytes(tranDataSource, this.tranDataByte, 1, tranDataLenth, 1);

        byte[] tranTimeSource = this.tranTime.getBytes();
        int tranTimeLenth = tranTimeSource.length;
        MsgUtils.copyBytes(tranTimeSource, this.tranTimeByte, 1, tranTimeLenth, 1);

        byte[] tranSnSource = this.tranSn.getBytes();
        int tranSnLenth = tranSnSource.length;
        MsgUtils.copyBytes(tranSnSource, this.tranSnByte, 1, tranSnLenth, 1);

        byte[] fileNameSource = this.fileName.getBytes();
        int fileNameLenth = fileNameSource.length;
        MsgUtils.copyBytes(fileNameSource, this.fileNameByte, 1, fileNameLenth, 1);

        byte[] macSource = this.mac.getBytes();
        int macLenth = macSource.length;
        MsgUtils.copyBytes(macSource, this.macByte, 1, macLenth, 1);

    }

    @Override
    public void initPropertiesByBytes(byte[] source) {
        if (source != null && source.length >= MSG_NOTIFICATION_LENGTH) {
            MsgUtils.copyBytes(source, this.senderIdByte, 1, 4, 1);
            MsgUtils.copyBytes(source, this.tranTypeByte, 5, 2, 1);
            MsgUtils.copyBytes(source, this.tranDataByte, 7, 8, 1);
            MsgUtils.copyBytes(source, this.tranTimeByte, 15, 6, 1);
            MsgUtils.copyBytes(source, this.tranSnByte, 21, 17, 1);
            MsgUtils.copyBytes(source, this.fileNameByte, 38, 60, 1);
            MsgUtils.copyBytes(source, this.macByte, 98, 40, 1);

            this.senderId = new String(this.senderIdByte);
            this.tranType = new String(this.tranTypeByte);
            this.tranData = new String(this.tranDataByte);
            this.tranTime = new String(this.tranTimeByte);
            this.tranSn = new String(this.tranSnByte);
            this.fileName = new String(this.fileNameByte);
            this.mac = new String(this.macByte);
        } else {
            logger.error("init NotificationMsg from bytes error: the bytes not enough");
        }
    }

    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getTranType() {
        return tranType;
    }

    public void setTranType(String tranType) {
        this.tranType = tranType;
    }

    public String getTranData() {
        return tranData;
    }

    public void setTranData(String tranData) {
        this.tranData = tranData;
    }

    public String getTranTime() {
        return tranTime;
    }

    public void setTranTime(String tranTime) {
        this.tranTime = tranTime;
    }

    public String getTranSn() {
        return tranSn;
    }

    public void setTranSn(String tranSn) {
        this.tranSn = tranSn;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

}